package top.haijunit.navi.auth.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import top.haijunit.framework.core.domain.QueryPage;
import top.haijunit.framework.core.domain.ResultPage;
import top.haijunit.framework.core.exception.ExceptionServer;
import top.haijunit.navi.auth.domain.PositionConvert;
import top.haijunit.navi.auth.domain.PositionInfo;
import top.haijunit.navi.auth.entity.PositionEntity;
import top.haijunit.navi.auth.mapper.PositionMapper;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 职位相关的业务处理
 *
 * @author zhanghaijun
 * @since 2024-10-28
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PositionService extends ServiceImpl<PositionMapper, PositionEntity> {

    private final PositionConvert converter;

    private static LambdaQueryWrapper<PositionEntity> queryConditional(PositionInfo info) {
        LambdaQueryWrapper<PositionEntity> query = new LambdaQueryWrapper<>();
        if (info != null) {
            query.like(CharSequenceUtil.isNotBlank(info.getPositionName()), PositionEntity::getPositionName, info.getPositionName());
            query.like(CharSequenceUtil.isNotBlank(info.getNickname()), PositionEntity::getNickname, info.getNickname());
        }
        query.orderByAsc(PositionEntity::getPositionLevel);
        return query;
    }

    /**
     * 获取职位分页
     */
    public ResultPage<PositionInfo> selectPageList(QueryPage queryPage, PositionInfo info) {
        Page<PositionEntity> entityPage = this.baseMapper.selectPage(new Page<>(Math.max(1, queryPage.getPage()), Math.max(1, queryPage.getSize())), queryConditional(info));
        return ResultPage.build(entityPage, converter::convertPost);
    }

    /**
     * 获取职位列表
     */
    public Collection<PositionInfo> selectList(PositionInfo info) {
        List<PositionEntity> entityList = this.baseMapper.selectList(queryConditional(info));
        return converter.convertPost(entityList);
    }

    /**
     * 获取职位详情
     */
    public PositionInfo selectDetail(Long code) {
        if (Objects.isNull(code)) {
            throw new ExceptionServer("数据不存在");
        }
        LambdaQueryWrapper<PositionEntity> query = new LambdaQueryWrapper<>();
        query.eq(PositionEntity::getPositionId, code);
        List<PositionEntity> entityList = this.baseMapper.selectList(query);
        if (CollUtil.isEmpty(entityList)) {
            throw new ExceptionServer("位置不存在");
        }
        if (entityList.size() > 1) {
            log.error("数据库数据异常，存在重复的异常数据: {} 重复", code);
        }
        return converter.convert(CollUtil.getFirst(entityList));
    }

    /**
     * 添加职位
     */
    public Long insert(PositionInfo positionInfo) {
        Assert.hasLength(positionInfo.getPositionName(), "职位名称不能为空");
        Assert.hasLength(positionInfo.getNickname(), "职位昵称不能为空");
        LambdaQueryWrapper<PositionEntity> query = new LambdaQueryWrapper<>();
        query.eq(PositionEntity::getPositionName, positionInfo.getPositionName());
        query.or().eq(PositionEntity::getNickname, positionInfo.getNickname());
        if (this.exists(query)) {
            throw new ExceptionServer("职位已存在");
        }
        PositionEntity positionEntity = converter.convert(positionInfo);
        this.baseMapper.insert(positionEntity);
        return positionEntity.getPositionId();
    }

    /**
     * 修改职位
     */
    public Long modify(PositionInfo positionInfo) {
        Assert.hasLength(positionInfo.getPositionId(), "修改的职位不存在");
        Assert.hasLength(positionInfo.getPositionName(), "职位名称不能为空");
        Assert.hasLength(positionInfo.getNickname(), "职位昵称不能为空");
        LambdaQueryWrapper<PositionEntity> query = new LambdaQueryWrapper<>();
        query.ne(PositionEntity::getPositionId, positionInfo.getPositionId());
        query.and(wrapper -> wrapper.eq(PositionEntity::getPositionName, positionInfo.getPositionName()).or().eq(PositionEntity::getNickname, positionInfo.getNickname()));
        if (this.exists(query)) {
            throw new ExceptionServer("职位已存在");
        }
        PositionEntity positionEntity = converter.convert(positionInfo);
        Assert.notNull(positionEntity.getPositionId(), "修改的职位不存在");
        LambdaQueryWrapper<PositionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionEntity::getPositionId, positionEntity.getPositionId());
        this.baseMapper.update(positionEntity, wrapper);
        return positionEntity.getPositionId();
    }

    /**
     * 删除职位
     */
    public Long delete(Long code) {
        LambdaQueryWrapper<PositionEntity> query = new LambdaQueryWrapper<>();
        query.eq(PositionEntity::getPositionId, code);
        this.baseMapper.delete(query);
        return code;
    }
}
